package algorithm.string;

import java.io.*;

/**
 * 序列化与反序列化
 */
public class SerializableDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        AA aa = new AA();
        aa.setStr("111");

        //序列化
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objOut = new ObjectOutputStream(byteArrayOutputStream);
        objOut.writeObject(aa);
        String str = byteArrayOutputStream.toString("ISO-8859-1");
        System.out.println(str);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
        ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream);
        System.out.println(((AA)inputStream.readObject()).toString());


    }
    static class AA implements Serializable {
        public String str;
        public void setStr(String s) {
            str = s;
        }
        public String getStr() {
            return this.str;
        }

        @Override
        public String toString() {
            return "str:"+str;
        }
    }
}
